#include "RsGisLibMorImageClosing.h"
#include "ExtensionManager.h"
#include "ExtensionSession.h"
#include <QVariant>

RsGisLibMorImageClosing::RsGisLibMorImageClosing(RsgisPortal* ClientPortal, QWidget *parent)
{
	ui = new Ui::RsGisLibMorImageClosing();
	ui->setupUi(this);

	Portal = ClientPortal;

	connect(ui->pushButton_InputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_InputFilePath_clicked()));
	connect(ui->pushButton_TempFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_TempFilePath_clicked()));
	connect(ui->pushButton_OperatorFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_OperateFilePath_clicked()));
	connect(ui->pushButton_OutputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_OutputFilePath_clicked()));
	connect(ui->pushButton_ImageClosing, SIGNAL(clicked()), this, SLOT(on_action_Button_ImageClosing_clicked()));

	ui->textBrowser_ParameterDescription->setText(
		"use_op_file - is a boolean specifying whether the morph_op_file file is present or whether a square operator (specified via op_size) should be used. (True = morph_op_file, False = op_size)\nop_size - is a integer specifying the square operator size (only used if use_op_file is False)\nniters - is an int for the number of iterations of the operators(Optional, default = 1)\nGdal Format - string specifying the output image format\ndatatype - is an int containing one of the values from rsgislib.TYPE_*"
	);

	ui->textBrowser_ParameterDescription->setEnabled(false);

	QStringList DatatypeList;
	DatatypeList.append("undefined");
	DatatypeList.append("8 Int");
	DatatypeList.append("16 Int");
	DatatypeList.append("32 Int");
	DatatypeList.append("64 Int");
	DatatypeList.append("8 uInt");
	DatatypeList.append("16 uInt");
	DatatypeList.append("32 uInt");
	DatatypeList.append("64 uInt");
	DatatypeList.append("32 Float");
	DatatypeList.append("64 Float");
	ui->comboBox_Datatype->addItems(DatatypeList);

	QStringList OperFileUseList;
	OperFileUseList.append("False");
	OperFileUseList.append("True");
	ui->comboBox_UseOperatorFile->addItems(OperFileUseList);

	connect(ui->comboBox_UseOperatorFile, SIGNAL(currentIndexChanged(int)), this, SLOT(currentIndexChange()));

	ui->lineEdit_InputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\Test.tif");
	ui->lineEdit_OperatorFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestOpSize.gmtxt");
	ui->lineEdit_TempFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestTemp");
	ui->lineEdit_OutputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestImageClosing");

	ui->lineEdit_MorphOpSize->setText("5");
	ui->lineEdit_IterNum->setText("1");
	ui->lineEdit_GdalFormat->setText("GTIFF");
	ui->comboBox_Datatype->setCurrentIndex(10);
	ui->comboBox_UseOperatorFile->setCurrentIndex(0);

	ui->lineEdit_OperatorFilePath->setEnabled(false);
	ui->pushButton_OperatorFilePath->setEnabled(false);
	ui->lineEdit_MorphOpSize->setEnabled(true);

	ui->lineEdit_Priority->setText("16");
}

void RsGisLibMorImageClosing::on_action_Button_InputFilePath_clicked()
{
	QString str = QFileDialog::getOpenFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_InputFilePath->setText(str.toUtf8());
}

void RsGisLibMorImageClosing::on_action_Button_TempFilePath_clicked()
{
	QString str = QFileDialog::getOpenFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_TempFilePath->setText(str.toUtf8());
}

void RsGisLibMorImageClosing::on_action_Button_OperateFilePath_clicked()
{
	QString str = QFileDialog::getOpenFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_OperatorFilePath->setText(str.toUtf8());
}

void RsGisLibMorImageClosing::on_action_Button_OutputFilePath_clicked()
{
	QString str = QFileDialog::getSaveFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_OutputFilePath->setText(str.toUtf8());
}

void RsGisLibMorImageClosing::on_action_Button_ImageClosing_clicked()
{
	{
		if (ui->lineEdit_InputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "DEM File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->comboBox_UseOperatorFile->currentIndex() == 0)
		{
			if (ui->lineEdit_MorphOpSize->text().isEmpty())
			{
				QMessageBox::warning(NULL, "Error", "Morph Op Size is Null", QMessageBox::Yes, QMessageBox::Yes);
				return;
			}
		}
		else
		{
			if (ui->lineEdit_OperatorFilePath->text().isEmpty())
			{
				QMessageBox::warning(NULL, "Error", "Operator File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
				return;
			}
		}

		if (ui->lineEdit_OutputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Output File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_GdalFormat->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Gdal Format is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Priority->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Priority is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}
	}

	QList<QVariant> SysParaList;
	SysParaList << QVariant(QString("RsGisLib"));
	SysParaList << QVariant(QString("Morphology"));
	SysParaList << QVariant(int(ui->lineEdit_Priority->text().toInt()));

	QList<QVariant> AlgParaList;
	AlgParaList << QVariant(QString("ImageClosing"));
	AlgParaList << QVariant(QString(ui->lineEdit_InputFilePath->text()));
	AlgParaList << QVariant(QString(ui->lineEdit_OutputFilePath->text()));
	AlgParaList << QVariant(QString(ui->lineEdit_TempFilePath->text()));
	AlgParaList << QVariant(int(ui->comboBox_UseOperatorFile->currentIndex()));
	if (ui->comboBox_UseOperatorFile->currentIndex() == 0)
	{
		AlgParaList << QVariant(int(ui->lineEdit_MorphOpSize->text().toInt()));
	}
	else
	{
		AlgParaList << QVariant(QString(ui->lineEdit_OperatorFilePath->text()));
	}
	AlgParaList << QVariant(int(ui->lineEdit_IterNum->text().toInt()));
	AlgParaList << QVariant(QString(ui->lineEdit_GdalFormat->text()));
	AlgParaList << QVariant(int(ui->comboBox_Datatype->currentIndex()));

	QVariantList ArgList;
	ArgList << (QVariant)SysParaList;
	ArgList << (QVariant)AlgParaList;

	Portal->goAhead(ArgList);
}

void RsGisLibMorImageClosing::currentIndexChange()
{
	if (ui->comboBox_UseOperatorFile->currentIndex() == 0)
	{
		ui->lineEdit_OperatorFilePath->setEnabled(false);
		ui->pushButton_OperatorFilePath->setEnabled(false);
		ui->lineEdit_MorphOpSize->setEnabled(true);
		ui->lineEdit_MorphOpSize->setText("5");
	}
	else
	{
		ui->lineEdit_OperatorFilePath->setEnabled(true);
		ui->pushButton_OperatorFilePath->setEnabled(true);
		ui->lineEdit_MorphOpSize->setEnabled(false);
		ui->lineEdit_OperatorFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestOpSize.gmtxt");
	}
}
